package com.tanhua.admin.listener;

// Log的监听器  处理消息


import com.tanhua.autoconfig.template.AliyunGreenTemplate;
import com.tanhua.dubbo.mongo.api.MovementApi;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


@Component
public class AuditListener {

    @Autowired
    private AliyunGreenTemplate aliyunGreenTemplate;

    @DubboReference
    private MovementApi movementApi;


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(
                            value = "tanhua.audit.queue"
                    ),
                    exchange = @Exchange(
                            value = "tanhua.audit.exchange",
                            type = ExchangeTypes.TOPIC
                    ),
                    key = "audit.movement"
            ))
    public void auditg(String movementId) {

        //解析传过来的数据

        try {
            System.out.println("movementId:++" +movementId);
            //根据movementId查询数据库数据
            Movement movement = movementApi.findByMovementId(movementId);

            String content = movement.getTextContent();
            List<String> medias = movement.getMedias();

            //用工具查询该动态的 文字和照片
            Map<String, String> scan = aliyunGreenTemplate.greenTextScan(content);

            Map<String, String> imageScan = aliyunGreenTemplate.imageScan(medias);

            //设置状态码：
            int state = 0;

            if (scan != null && imageScan != null) {

                String scanStatus = scan.get("suggestion");
                String imageStatus = imageScan.get("suggestion");

                if ("pass".equals(scanStatus) && "pass".equals(imageStatus)) {
                    state = 1;
                } else if ("pass".equals(scanStatus) || "pass".equals(imageStatus)) {
                    state = 2;
                }
            }

            System.out.println("状态码:++++++++"+state);


            movementApi.updateByState(movementId, state);


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
